package com.wu.javax.servlet.http;

import com.wu.javax.servlet.Servlet;
import com.wu.javax.servlet.ServletRequest;
import com.wu.javax.servlet.ServletResponse;

/**
 * 适配器模式
 *
 * @author 吴磊
 */
public abstract class HttpServlet implements Servlet {
    @Override
    public void init() {
    }

    @Override
    public void destroy() {
    }

    /**
     * 处理 get 请求
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    }

    /**
     * 处理 post 请求
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    }

    /**
     * 处理 head 请求
     *
     * @para @param response HttpServletResponse
     */
    protected void doHead(HttpServletRequest request, HttpServletResponse response) {
    }

    /**
     * 处理 delete 请求
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) {
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) {
        this.service((HttpServletRequest) request, (HttpServletResponse) response);
    }

    public void service(HttpServletRequest request, HttpServletResponse response) {
        //取出 request中的method
        String method = request.getMethod();
        //判断是get/post 然后调子类的方法处理即可
        if ("get".equalsIgnoreCase(method)) {
            doGet(request, response);
        } else if ("post".equalsIgnoreCase(method)) {
            doPost(request, response);
        } else if ("head".equalsIgnoreCase(method)) {
            doHead(request, response);
        } else if ("delete".equalsIgnoreCase(method)) {
            doDelete(request, response);
        }
    }
}
